package solution;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-01-21
 * Time: 20:27
 */

import java.util.HashSet;
import java.util.Set;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode(int x) { val = x; }
 * }
 */

//https://leetcode.cn/problems/remove-duplicate-node-lcci/submissions/396594580/
class Solution6 {
    public ListNode removeDuplicateNodes(ListNode head) {
        if (head == null) {
            return null;
        }
        Set<Integer> hs = new HashSet<>();
        hs.add(head.val);
        ListNode cur = head;
        while (cur.next != null) {
            if (!hs.add(cur.next.val)) {
                cur.next = cur.next.next;
            } else {
                cur = cur.next;
            }
        }
        return head;
    }
}
